package com.hc.comm.Service;

import com.hc.bean.RoleBean;
import com.hc.radius.service.DataParse;
import com.hc.radius.service.Order;

public class DataParseService implements DataParse {

	private final static String RETN = "RESULT";
	@Override
	public RoleBean parseToPPPOEUSER(String data) {
		if (!data.contains(RETN)) {
			return null;
		}
		data = data.substring(data.indexOf(RETN) + 8, data.lastIndexOf(";") - 2);
		String[] datas = data.split("\\|");
		RoleBean rb = new RoleBean();
		//用户名
		rb.setNumber(datas[0]);
		//ip
		rb.setIntranetIp(datas[1]);
        //	接入设备IP	
		rb.setExternalIp(datas[2]);
		//接入端口ID
		String[] vlanids = datas[3].split(";");
        if (vlanids.length >= 4) {
        	String[] vlanid = vlanids[3].split("=");
        	if (vlanid.length == 2) {
        		rb.setVlanId1(vlanid[1]);
        	}
        } 
        if (vlanids.length>= 5) {
        	String[] vlanid = vlanids[4].split("=");
        	if (vlanid.length == 2) {
        		rb.setVlanId2(vlanid[1]);
        	}
        }
		
		//域名
		rb.setCompany(datas[7]);
		//接入设备端口
		
//		mac地址
//		rb.setAddress(datas[12]);
		
		return rb;
	}

	@Override
	public Order parseToOrder(String data) {
		if (!data.contains(RETN)) {
        	return null;
        }
        data = data.substring(data.indexOf(RETN)+8,data.lastIndexOf(";") - 2);
        String[] datas = data.split("\\|");
        Order order = new Order();
        String name = datas[4];
        order.setId(datas[1]);
        order.setName(name);
        order.setBroadbandSize(getBroadbandSize(name));
		return order;
	}
	
	
	private String getBroadbandSize(String name) {
		if (!name.contains("M")) {
			return "false";
		}
		String result = "";
		int index = name.indexOf("M") - 1;
		for (int i = index ; i >= 0; i--) {
			char c = name.charAt(i);
			if (c < 48 || c > 57) {
				break;
			}
			result = c + result;
		}
		return result;
	}
	
	public boolean handler(String content) {

		boolean status = false;
		if (content.contains("ACK:LOGIN:")) {
			if (content.contains("RETN=0")) {
				status = true;
			} 
		} else if (content.contains("ACK:LOGOUT:")) {
			if (content.contains("RETN=0")) {
				status = true;
			}
		} else if (content.contains("ACK:QUERY C280 USERSESS:")) {
			if (content.contains("RETN=0")) {
				status = true;
			}

		} else if (content.contains("ACK:QUERY C280 ORDER:")) {
			if (content.contains("RETN=0")) {
				status = true;
			}
		}
		return status;
	
	}
 
}
